package com.cyys.modules.system.service.param;

import com.cyys.common.ql.AR;
import com.cyys.common.services.CyysBaseService;
import com.cyys.common.utils.date.DateUtil;
import com.cyys.modules.system.dao.param.ParamHolidayDao;
import com.cyys.modules.system.model.param.ParamHoliday;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service("paramHolidayService")
@Transactional(readOnly = true)
public class ParamHolidayService extends CyysBaseService<ParamHolidayDao, ParamHoliday> {
	/**
	 * 根据条件查询所有数据
	 * 
	 * @param paramHoliday
	 * @param isDelete
	 *            null,查询所有数据 0:查询未删除的数据,1:查询已删除的数据
	 * @return
	 * @throws Exception
	 */
	public List<ParamHoliday> listAll(ParamHoliday paramHoliday, Integer isDelete) throws Exception{
		return dao.listByCondition(paramHoliday, isDelete);
	}

	
	@Transactional(readOnly = false)
	public AR physicsDeleteByDate(String date)  throws Exception{
		if (date == null){
			return AR.failed("date为空");
		}else{
			dao.coerceDeleteByDate(date);
			return AR.ok("取消节假日成功");
		}
	}
	
	/**
	 * 获取单个对象
	 * 
	 * @param date
	 * @return
	 */
	public ParamHoliday getByDate(String date) throws Exception{
		return dao.getByDate(date);
	}

	@Transactional(readOnly = false)
	public AR createHolidayForYear(String currentYear) throws Exception{
		if(currentYear == null){
			currentYear = String.valueOf(DateUtil.getYear(new Date()));
		}
		//删除本年所有数据
		dao.coerceDeleteByYear(currentYear);
		Date start = DateUtil.StringToDate(currentYear + "0101","yyyyMMdd");//开始时间
        Date end = DateUtil.StringToDate(currentYear + "1231","yyyyMMdd");//结束时间 
        List<Date> lists = dateSplit(start, end);
        if (!lists.isEmpty()) {  
            for (Date date : lists) {
				if (DateUtil.isWeekend(date)) {
					Calendar cal = Calendar.getInstance();
					cal.setTime(date);
					// System.out.println("********插入日期:" + sdf.format(date) +
					// "***********");
					ParamHoliday paramHoliday = new ParamHoliday();
					
					int year = cal.get(Calendar.YEAR);
					int month = cal.get(Calendar.MONTH) + 1;
					int day = cal.get(Calendar.DATE);
					
					String holidayDate = year + "-" + month + "-" + day;
					paramHoliday.setDateCreate(new Date());
					paramHoliday.setHolidayDate(holidayDate);
					paramHoliday.setHolidayDay(String.valueOf(day));
					paramHoliday.setHolidayMonth(String.valueOf(month));
					paramHoliday.setHolidayYear(String.valueOf(year));
					paramHoliday.setHolidayType("1");
					dao.insert(paramHoliday);
				}
            }
        }
		return AR.ok("初始化节假日成功");
	}
	
	private static List<Date> dateSplit(Date start, Date end) throws Exception {  
        if (!start.before(end))  
            throw new Exception("开始时间应该在结束时间之后");  
        Long spi = end.getTime() - start.getTime();  
        Long step = spi / (24 * 60 * 60 * 1000);// 相隔天数  
  
        List<Date> dateList = new ArrayList<Date>();  
        dateList.add(end);  
        for (int i = 1; i <= step; i++) {  
            dateList.add(new Date(dateList.get(i - 1).getTime() - (24 * 60 * 60 * 1000)));// 比上一天减一  
        }  
        return dateList;  
    }  
	
}
